package com.tx.blog.article.controller;


import com.tx.blog.article.service.IArticleService;
import com.tx.blog.article.vo.ArticleTypeVo;
import com.tx.blog.article.vo.ArticleVo;
import com.tx.blog.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author tx
 * @description
 * @date  2019/4/26 16:03
 * @param  * @Param: null
 * @return 
**/
@Controller
@RequestMapping("/articleController")
public class ArticleController {

    @Autowired
    IArticleService articleService;
    
    @RequestMapping("/showArticles")
    public ModelAndView showArticles(HttpServletRequest request){
        ModelAndView mv = getModelAndView(request, "article/showArticles");
        return mv;
    }

    @RequestMapping("/manageArticles")
    public ModelAndView manageArticles(HttpServletRequest request){
        ModelAndView mv = getModelAndView(request, "article/manageArticles");
        return mv;
    }

    /**
     * 抽取公共
     * @param request
     * @param s
     * @return
     */
    private ModelAndView getModelAndView(HttpServletRequest request, String s) {
        String userId = HttpUtil.getCurrentUserId(request);
        ModelAndView mv = new ModelAndView(s);
        List<ArticleVo> articleList = articleService.findAll(userId);
        mv.addObject("articles", articleList);
        return mv;
    }


    @RequestMapping("/jumpToAddView")
    public ModelAndView jumpToAddView(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("article/addView");
        //查找当前用户拥有的文章类型
        String userId = HttpUtil.getCurrentUserId(request);
        List<ArticleTypeVo> typeList = articleService.findAllArticleType(userId);
        mv.addObject("typeList",typeList);
        return mv;
    }

    @RequestMapping("saveArticle")
    public ModelAndView saveArticle(ArticleVo articleVo,HttpServletRequest request){
        String userId = HttpUtil.getCurrentUserId(request);
        ModelAndView mv = new ModelAndView("redirect:/articleController/manageArticles");
        boolean flag = articleService.saveArticle(userId,articleVo);
        if(!flag){
            mv = new ModelAndView("redirect:/articleController/jumpToAddView");
        }
        return mv;
    }


    @RequestMapping("deleteArticle")
    public ModelAndView deleteArticle(ArticleVo articleVo,HttpServletRequest request){
        String userId = HttpUtil.getCurrentUserId(request);
        ModelAndView mv = new ModelAndView("redirect:/articleController/manageArticles");
        articleService.deleteArticle(userId,articleVo);
        return mv;
    }

    @RequestMapping("/updateArticle")
    public ModelAndView updateArticle(ArticleVo articleVo,HttpServletRequest request){
        String userId = HttpUtil.getCurrentUserId(request);
        ModelAndView mv = new ModelAndView("redirect:/articleController/manageArticles");
        articleService.updateArticle(userId,articleVo);
        return mv;
    }

    @RequestMapping("/jumpToEditView")
    public ModelAndView jumpToEditView(String articleId,HttpServletRequest request){
        ModelAndView mv = new ModelAndView("article/editView");
        ArticleVo articleVo = articleService.findArticle(articleId);
        mv.addObject("articleVo",articleVo);
        String userId = HttpUtil.getCurrentUserId(request);
        List<ArticleTypeVo> typeList = articleService.findAllArticleType(userId);
        mv.addObject("typeList",typeList);
        return mv;
    }



    //----------------文章类型-----------------

    @RequestMapping("/addNewType")
    public ModelAndView addNewType(ArticleTypeVo articleTypeVo){
        ModelAndView mv = new ModelAndView("redirect:/articleController/findAllArticleType");
        articleService.addNewType(articleTypeVo);
        return mv;
    }

    @RequestMapping("/findAllArticleType")
    @ResponseBody
    public List<ArticleTypeVo> findAllArticleType(HttpServletRequest request){
        String userId = HttpUtil.getCurrentUserId(request);
        return articleService.findAllArticleType(userId);
    }

}
